package net.begincode.admin.controller;

import net.begincode.admin.bean.UserIdParam;
import net.begincode.auth.UserInfo;
import net.begincode.biz.UserBiz;
import net.begincode.blog.UserContext;
import net.begincode.blog.bean.param.UserQueryParam;
import net.begincode.blog.bean.vo.CheckBlog;
import net.begincode.blog.entity.User;
import net.begincode.blog.enums.UserFlagEnum;
import net.begincode.common.ApiResponse;
import net.begincode.design.BizTemplate;
import net.begincode.page.PageRequest;
import net.begincode.page.PageResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.annotation.processing.SupportedSourceVersion;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户管理
 */
@Controller
@RequestMapping("/admin/user/")
public class UserController {
    @Resource
    private UserBiz userBiz;

    /**
     * 查询未审核用户
     */
    @ResponseBody
    @RequestMapping(value = "/queryWaitCheckUser", method = RequestMethod.GET)
    public ApiResponse<List<User>> queryWaitCheckUser(UserQueryParam userQueryParam) {

        return new BizTemplate<ApiResponse<List<User>>>("UserController.queryWaitCheckUser") {
            @Override
            public void checkParam() {
                super.checkParam();
            }

            @Override
            public ApiResponse<List<User>> process() {
                PageResponse<User> pager = userBiz.query(userQueryParam);
                return ApiResponse.success(pager.getData(), pager.getTotalSize());

            }
        }.excute();

    }

    /**
     * 审核通过
     */
    @ResponseBody
    @RequestMapping(value = "checkPass", method = RequestMethod.POST)
    public ApiResponse<Boolean> checkUser(UserIdParam userIdParam) {
        boolean checkFlag = userBiz.updateUserFlag(userIdParam.getUserId(), UserFlagEnum.EFFECT.getFlagCode());
        if (checkFlag) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }

    /**
     * 审核不通过
     */
    @ResponseBody
    @RequestMapping(value = "checkFailed", method = RequestMethod.POST)
    public ApiResponse<Boolean> checkFailedUser(UserIdParam userIdParam) {
        boolean checkFlag = userBiz.updateUserFlag(userIdParam.getUserId(), UserFlagEnum.UNEFFECT.getFlagCode());
        if (checkFlag) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }

    /**
     * 冻结
     */
    @ResponseBody
    @RequestMapping(value = "frozen", method = RequestMethod.POST)
    public ApiResponse<Boolean> frozenUser(UserIdParam userIdParam) {
        boolean checkFlag = userBiz.updateUserFlag(userIdParam.getUserId(), UserFlagEnum.FROZEN.getFlagCode());
        if (checkFlag) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }

    @RequestMapping("login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        UserInfo userInfo = new UserInfo();
        userInfo.setId("1");
        userInfo.setToken("");
        UserContext.setUserInfo(userInfo);
        modelAndView.setViewName("admin");
        return modelAndView;
    }
}
